/**
 * 
 */
package com.tw.trips.domain;

import java.util.Date;
import java.util.Set;

import org.neo4j.graphdb.Direction;
import org.springframework.data.neo4j.annotation.Fetch;
import org.springframework.data.neo4j.annotation.GraphId;
import org.springframework.data.neo4j.annotation.NodeEntity;
import org.springframework.data.neo4j.annotation.RelatedTo;
import org.springframework.data.neo4j.template.Neo4jOperations;

/**
 * @author m1005264
 * 
 */
@NodeEntity
public class Trip {

	@GraphId
	private Long nodeiD;

	private Location source;

	private Location destination;

	private Date travelStartDate;

	private Date travelEndDate;

	@RelatedTo(type = "CREATED_BY", elementClass = Traveller.class, direction = Direction.INCOMING)
	@Fetch
	private Traveller createdBy;

	public Trip() {
		super();
	}

	public Trip(Location source, Location destination, Date travelStartDate,
			Date travelEndDate, Set<Traveller> travellers, Itinerary itnary,
			Date createdOn) {
		this.source = source;
		this.destination = destination;
		this.travelStartDate = travelStartDate;
		this.travelEndDate = travelEndDate;
		this.travellers = travellers;
		this.itinerary = itnary;
		this.createdOn = createdOn;
	}

	private Set<Traveller> travellers;

	private Itinerary itinerary;

	@RelatedTo(type = "ACTIVITIES", elementClass = Activity.class)
	@Fetch
	private Set<Activity> activities;

	public Trip addActivity(Neo4jOperations operations, Activity activity) {
		this.activities.add(activity);
		return operations.save(this);
	}

	public Trip addActivity(Neo4jOperations operations, Traveller updatedBy,
			Location locn, Date createdOn, String msg, String imgURL) {
		Update update = new Update(msg, imgURL, createdOn);
		update.setForLocation(locn);
		update.setUpdatedBy(updatedBy);
		// update.setTrip(this);
		return operations.save(this);
	}

	private Date createdOn;

	public Long getNodeiD() {
		return nodeiD;
	}

	public void setNodeiD(Long nodeiD) {
		this.nodeiD = nodeiD;
	}

	public Location getSource() {
		return source;
	}

	public void setSource(Location source) {
		this.source = source;
	}

	public Location getDestination() {
		return destination;
	}

	public void setDestination(Location destination) {
		this.destination = destination;
	}

	public Date getTravelStartDate() {
		return travelStartDate;
	}

	public void setTravelStartDate(Date travelStartDate) {
		this.travelStartDate = travelStartDate;
	}

	public Date getTravelEndDate() {
		return travelEndDate;
	}

	public void setTravelEndDate(Date travelEndDate) {
		this.travelEndDate = travelEndDate;
	}

	public Traveller getCreatedBy() {
		return createdBy;
	}

	public void setCreatedBy(Traveller createdBy) {
		this.createdBy = createdBy;
	}

	public Set<Traveller> getTravellers() {
		return travellers;
	}

	public void setTravellers(Set<Traveller> travellers) {
		this.travellers = travellers;
	}

	public Set<Activity> getActivities() {
		return activities;
	}

	public Date getCreatedOn() {
		return createdOn;
	}

	public void setCreatedOn(Date createdOn) {
		this.createdOn = createdOn;
	}

	/**
	 * @return the itinerary
	 */
	public Itinerary getItinerary() {
		return itinerary;
	}

	/**
	 * @param itinerary
	 *            the itinerary to set
	 */
	public void setItinerary(Itinerary itinerary) {
		this.itinerary = itinerary;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "Trip [nodeiD=" + nodeiD + ", source=" + source
				+ ", destination=" + destination + ", travelStartDate="
				+ travelStartDate + ", travelEndDate=" + travelEndDate
				+ ", createdBy=" + createdBy + ", travellers=" + travellers
				+ ", itinerary=" + itinerary + ", activities size ="
				+ (activities != null ? activities.size() : 0) + ", createdOn="
				+ createdOn + "]";
	}

	// public void addActivity(Update activity) {
	// activity.setTrip(this);
	// }

}
